package xyf.p07.binsearch;

/**
 * ClassName: P0744_NextGreatesLetter
 * Description:
 * Author: xyf
 * Date: 9/7/22 3:54 PM
 * Version: 1.0
 **/
public class P0744_NextGreatesLetter
{
    //寻找第一个大于目标数的，处理等于的情况时start = middle + 1
    public char nextGreatestLetter(char[] letters, char target)
    {
        int start = 0;
        int end = letters.length - 1;
        while (start <= end)
        {
            int middle = (end - start) / 2 + start;

            if (target >= letters[middle])
            {
                start = middle + 1;
            }
            else
            {
                if (middle == 0 || letters[middle - 1] <= target)
                {
                    return letters[middle];
                }
                else
                {
                    end = middle - 1;
                }
            }
        }

        return letters[0];
    }
}